<template>
  <div class="app-container calendar-list-container">
    <el-row>
      <el-col :span="12">
        <div class="grid-content bg-purple">
          <el-form :model="ruleForm2" :rules="rules2" ref="ruleForm2" label-width="100px" class="demo-ruleForm">
            <el-form-item label="用户名" prop="username">
              <el-input type="text" :value="userInfo.username" disabled></el-input>
            </el-form-item>
            <el-form-item label="原密码" prop="password">
              <el-input type="password" v-model="ruleForm2.password" placeholder="请输入原密码" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="newpassword1">
              <el-input type="password" v-model="ruleForm2.newpassword1" placeholder="请输入新密码" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="newpassword2">
              <el-input type="password" v-model="ruleForm2.newpassword2" placeholder="请确认密码" auto-complete="off" ></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="ruleForm2.phone" placeholder="请输入手机号码"></el-input>
            </el-form-item>
            <el-form-item label="头像">
              <my-upload field="file" 
                  @crop-upload-success="cropUploadSuccess"
                  v-model="show" 
                  :width="300" 
                  :height="300" 
                  url="/upms/user/upload" 
                  :headers="headers" 
                  :withCredentials="false"
                  img-format="png">
              </my-upload>
              
              <img class="headImg" @click="toggleShow" v-bind:src="ruleForm2.avatar" :onerror="defluatImg">
              <el-button type="primary" @click="toggleShow" size="mini">选择
                <i class="el-icon-upload el-icon--right"></i>
              </el-button>
             
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm2')">提交</el-button>
              <el-button @click="resetForm('ruleForm2')">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import myUpload from 'vue-image-crop-upload'
import { getToken } from '@/util/auth'
import ElFormItem from 'element-ui/packages/form/src/form-item.vue'
import request from '@/router/axios'

export default {
  components: {
    ElFormItem,
    'my-upload': myUpload
  },
  data() {
    var validatePass = (rule, value, callback) => {
      if (this.ruleForm2.newpassword1 !== '') {
        if (value === '') {
          callback(new Error('请输入密码'))
        } else if (value.length < 6) {
          callback(new Error('密码不能小于6位'))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (this.ruleForm2.newpassword1 !== '') {
        if (value === '') {
          callback(new Error('请再次输入密码'))
        } else if (value !== this.ruleForm2.newpassword2) {
          callback(new Error('两次输入密码不一致!'))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
    return {
      defluatImg: 'this.src = "' + require('../../../../static/img/avator.gif') + '"',
      fileList: [],
      show: false,
      headers: {
        Authorization: 'Bearer ' + getToken()
      },
      ruleForm2: {
        password: '',
        newpassword1: '',
        newpassword2: '',
        avatar: '',
        phone: ''
      },
      rules2: {
        password: [{
          min: 6,
          message: '密码不能小于6位',
          trigger: 'blur'
        }],
        newpassword1: [{ validator: validatePass, trigger: 'blur' }],
        newpassword2: [{ validator: validatePass2, trigger: 'blur' }]
      }
    }
  },
  created() {
    this.ruleForm2.avatar = this.userInfo.avatar
    this.ruleForm2.phone = this.userInfo.phone
  },
  computed: {
    ...mapState({
      userInfo: state => state.user.userInfo
    })
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          request({
            url: '/upms/user/editInfo',
            method: 'put',
            data: this.ruleForm2
          }).then(response => {
            if (response.data.data) {
              this.$store.commit('DEL_ALL_TAG')
              this.$router.push({ path: '/' })
              this.userInfo.phone = this.ruleForm2.phone
              this.userInfo.avatar = this.ruleForm2.avatar
              // 修改密码之后强制重新登录
              if (this.ruleForm2.newpassword1 !== '') {
                this.$alert('密码修改成功,请重新登录!', '提示', {
                  confirmButtonText: '确定',
                  callback: action => {
                    this.$store.dispatch('LogOut').then(() => {
                      location.reload() // 为了重新实例化vue-router对象 避免bug
                    })
                  }
                })
              } else {
                this.$notify({
                  title: '成功',
                  message: '修改成功',
                  type: 'success',
                  duration: 2000
                })
              }
            } else {
              this.$notify({
                title: '失败',
                message: response.data.msg,
                type: 'error',
                duration: 2000
              })
            }
          }).catch(() => {
            this.$notify({
              title: '失败',
              message: '修改失败',
              type: 'error',
              duration: 2000
            })
          })
        } else {
          return false
        }
      })
    },
    resetForm(formName) {
      console.log(formName)
      this.$refs[formName].resetFields()
    },
    toggleShow() {
      this.show = !this.show
    },
    /**
     * upload success
     * [param] jsonData   服务器返回数据，已进行json转码
     * [param] field
     */
    cropUploadSuccess(jsonData, field) {
      this.ruleForm2.avatar = jsonData.filename
    }
  }
}
</script>
